/*
* $Id: ViewPoints.cpp 70 2010-03-20 20:21:36Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ViewPoints.h"
#include "Database/Tables/PointsTable.h"
#include "Settings/Journal/PointViewSettings.h"
#include "Settings/DinIntDefined.h"
#include "Settings/SettingControlIds.h"
#include "ViewFieldValuesHelper.h"
#include "Event/EventManager.h"
#include "Event/EventId.h"

CViewPoints::CViewPoints(CState* pState)
: CViewTable(pState){
	IApplication::GetApp()->GetEventManager()->SubscribeEvent(this, POINTS_DB_CHANGE_EVENT);
	IApplication::GetApp()->GetEventManager()->SubscribeEvent(this, POINT_STN_CHANGE_EVENT);
	Init();
}
CViewPoints::~CViewPoints(){
	IApplication::GetApp()->GetEventManager()->UnSubscribeEvent(this, POINTS_DB_CHANGE_EVENT);
	IApplication::GetApp()->GetEventManager()->UnSubscribeEvent(this, POINT_STN_CHANGE_EVENT);
}

bool CViewPoints::IsViewState(State state){
	return state.m_nPosTab == JournalEnum::INSPECT_JOURNAL &&
		state.m_type == JournalEnum::NONE &&
		state.m_nTypeSel == JournalEnum::NOTSEL;
}

void CViewPoints::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_POINT;
	m_pState->m_nIdParameter = 0;
}
void CViewPoints::Init(){
	m_tableRegion.GetTableDrawer()->ResetTable();
	tableHeader *nameHeader = NULL, *noteHeader = NULL;
	nameHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_POINT_NAME_ID), TABLE_POINT_NAME_ID);
	CPointViewSettings pSettings;
	if(pSettings.GetState(CHECK_NOTE_ID) == CHECK_BOX_STATE_1)
		noteHeader = m_tableRegion.GetTableDrawer()->AddHeader(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), TABLE_POINT_NOTE_ID);
	CViewFieldValuesHelper helper(m_tableRegion.GetTableDrawer());
	helper.GenerateUserHeaders(CViewFieldValuesHelper::POINTS, TABLE_POINT_USER_ID);
	m_tableRegion.GetTableDrawer()->NormalizeHeader();
	IDatabase* db = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CPointsTable pointsTable;
	IResult* pResult = pointsTable.GetValue(db);
	for(int i = 0; i < (int)pResult->GetSizeResult(); i++){
		CPointsTable::content* pContent = (CPointsTable::content*)(*pResult)[i];
		CTableCelRow *row = new CTableCelRow(pContent->s_nId, JournalEnum::VIEW_POINT);
		row->AddCel(nameHeader, pContent->s_strName);
		if(noteHeader)
			row->AddCel(noteHeader, pContent->s_strNote);
		helper.GenerateUserCels(row, pContent->s_nIdRecord);
		m_tableRegion.GetTableDrawer()->AddCelRow(row);
	}
	pResult->Free();
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(db);
}
void CViewPoints::SetManipulation(JournalEnum::typeItemMnpl type, int nId){
	IWnd* pWnd = IApplication::GetApp()->GetVisualManager()->
		GetVisualComponent(IApplication::GetApp()->
		GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame();
	pWnd->Post(UM_ITEM_MANIPULATION, type, nId);
}

BEGIN_MESSAGE_MAP(CViewPoints, CViewTable)
	ON_COMMAND(BUTTON_ADD_ID, OnAddPoint)
	ON_COMMAND(BUTTON_DELETE_ID, OnDeletePoint)
	ON_COMMAND(BUTTON_CHANGE_ID, OnChangePoint)
END_MESSAGE_MAP()

void CViewPoints::OnAddPoint(){
	SetManipulation(JournalEnum::ADD_POINT, 0);
}
void CViewPoints::OnDeletePoint(){
	std::tstring strContent = IApplication::GetApp()->GetLanguageManager()->GetString(QUESTION_DELETE_ID) +
		_T(" '") + m_tableRegion.GetTableDrawer()->GetSelectRow()->GetValueColumn(TABLE_POINT_NAME_ID) + _T("'.");
	if(MessageBox(strContent.c_str(), IApplication::GetApp()->GetLanguageManager()->
		GetString(DELETE_POINT_ID).c_str(), MB_YESNO) == IDYES)
			SetManipulation(JournalEnum::DELETE_POINT, m_tableRegion.GetTableDrawer()->GetSelectRow()->GetId());
}
void CViewPoints::OnChangePoint(){
	SetManipulation(JournalEnum::CHANGE_POINT, m_tableRegion.GetTableDrawer()->GetSelectRow()->GetId());
}

void CViewPoints::EventHandler(){
	Init();
	if(IsCreate())
		Invalidate(FALSE);
}